package com.example.lenovo.contentprovide;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * Created by Lenovo on 2017/10/21.
 */

public class MyProvider extends ContentProvider {
    private MyOpenHelper openHelper;

    @Override
    public boolean onCreate() {
        openHelper = new MyOpenHelper(getContext());

        return true;
    }


    @Override
    public Cursor query( Uri uri,  String[] projection,  String selection,  String[] selectionArgs,  String sortOrder) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.query("info", projection, selection, selectionArgs, sortOrder);
        //  查询是不可关db 否则cursor会报错
        return cursor;
    }

    @Override
    public String getType( Uri uri) {
        return null;
    }

    @Override
    public Uri insert( Uri uri,  ContentValues values) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        long insert = db.insert("info", null, values);
        return Uri.parse(String.valueOf(insert));
    }

    @Override
    public int delete( Uri uri,  String selection,  String[] selectionArgs) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        int delete = db.delete("info", selection, selectionArgs);
        return delete;
    }

    @Override
    public int update( Uri uri,  ContentValues values,  String selection, String[] selectionArgs) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        int update = db.update("info", values, selection, selectionArgs);
        return update;
    }
}
